import { getAdminInfo } from '@/api/backend/admin';
import router from '@/router';
import { defineStore } from 'pinia';
export const useAdminStore = defineStore('admin', {
    state: () => {
        return {
            token: '',
            adminInfo: ''
        };
    },
    actions: {
        setToken(token) {
            this.token = token;
        },
        clearToken() {
            this.token = '';
            this.adminInfo = '';
            localStorage.removeItem('txmabc_admin');
        },
        setAdminInfo(adminInfo) {
            this.adminInfo = adminInfo;
        },
        async requestAdminInfo() {
            // console.log('store：' + this.token);
            if (this.token) {
                return new Promise((resolve, reject) => {
                    getAdminInfo().then(async (response) => {
                        if (!response || !response.data) {
                            this.clearToken();
                            await router.push({ name: 'admin_login' });
                            reject(false);
                        } else {
                            this.setAdminInfo(response.data);
                            resolve(response.data);
                        }
                    });
                });
            } else {
                await router.push({ name: 'admin_login' });
                return false;
            }
        }
    },
    getters: {
        isAuthenticated: (state) => !!state.token
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'txmabc_admin',
                storage: localStorage
            }
        ]
    }
});
